﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace PhoneApp1
{
    public partial class Poll : PhoneApplicationPage
    {
        public Poll()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string errorString = "";
            double salary = 0; 
            try
            {
                salary = Double.Parse(salaryTextBox.Text);
            }
            catch (Exception)
            {
                errorString += "הכנס משכורת תקינה\n";
            }

            double seniority = 0;
            try
            {
                seniority = Double.Parse(seniorityTextBox.Text);
            }
            catch (Exception)
            {
                errorString += "הכנס וותק תקין\n";
            }

            
            string comapny = companyTextBox.Text;
            if (comapny.Equals(""))
            {
                errorString += "הכנס שם חברה\n";
            }
            string desipline = deciplineTextBox.Text;
            if (desipline.Equals(""))
            {
                errorString += "הכנס שם תחום\n";
            }

            string work = workTextBox.Text;
            if (work.Equals(""))
            {
                errorString += "הכנס שם העבודה שלך\n";
            }

            if (String.IsNullOrEmpty(errorString))
            {

                string data = "seniority=" + seniority + "&jobTitle=" + work + "&company=" + comapny + "&discipline=" + desipline + "&salary=" + salary;
                
               
                httpPostData(data);

            }
            else
            {
                MessageBox.Show(errorString, "מחשבון הנטו", MessageBoxButton.OK);
            }
        }

        private void httpPostData(string data)
        {
            WebClient webClient = new WebClient();
            //webClient.Headers[HttpRequestHeader.ContentType] = "text/plain";
            var uri = new Uri("http://urielfra.appspot.com/testgae", UriKind.Absolute);
            //webClient.Headers[HttpRequestHeader.ContentLength] = data.Length.ToString();
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            //webClient.AllowWriteStreamBuffering = true;
            webClient.Encoding = System.Text.Encoding.UTF8;
            
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(postComplete);
            webClient.UploadStringAsync(uri, "POST", data);
           
            
            PollButton.IsEnabled = false;
        }




        private void postComplete(object sender, UploadStringCompletedEventArgs e)
        {


            try
            {
                if (String.IsNullOrEmpty(e.Result))
                {
                    MessageBox.Show("המשכורת נשלחה בהצלחה. תודה רבה", "מחשבון הנטו", MessageBoxButton.OK);
                }
                else
                {
                    MessageBox.Show("שליחת המידע נכשלה, נסו מאוחר יותר", "מחשבון הנטו", MessageBoxButton.OK);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("שליחת המידע נכשלה, נסו מאוחר יותר", "מחשבון הנטו", MessageBoxButton.OK);
            }
            finally
            {
                NavigationService.GoBack();
            }
                        
        }
    }
}